﻿using Abp.AutoMapper;
using Abp.Domain.Repositories;
using Abp.Net.Mail;
using Abp.Runtime.Caching;
using Abp.UI;
using Microsoft.AspNetCore.Http;
using Mt.Site.Application.ContentManagement.FrontEnd;
using Mt.Site.Application.ContentManagement.FrontEnd.Dto;
using Mt.Site.Application.Messages_.FrontEnd.Dto;
using Mt.Site.Core.Base.Domain.Entities.ShortId;
using Mt.Site.Core.Base.Utils.FileIO;
using Mt.Site.Core.Messages_;
using Mt.Site.Core.Security;
using Mt.Site.Core.Storage;
using System;
using System.Threading.Tasks;

namespace Mt.Site.Application.Messages_.FrontEnd
{
    public class MessagesFrontAppService : IMessagesFrontAppService
    {
        private readonly IContent2FrontEndAppService _content2; //内容数据接口
        private readonly IMessagesManager _messagesManager;
        private readonly ICacheManager _cacheManager;
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IEmailSender _sendEmail;                               // 邮件发送接口
        public MessagesFrontAppService(
          IRepository<Messages, Guid> messagesRepositor,
          IMessagesManager messagesManager, IContent2FrontEndAppService content2,
          ICacheManager cacheManager,
            IHttpContextAccessor httpContextAccessor, IEmailSender sendEmail)
        {
            _content2 = content2;
            _messagesManager = messagesManager;
            _cacheManager = cacheManager;
            _httpContextAccessor = httpContextAccessor;
            _sendEmail = sendEmail;
        }

        /// <summary>
        /// 表单提交
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// 

        public async Task<ResultType> RegisterMessages(MessageInput input)
        {
            //获取验证码
            //string sessionId = _httpContextAccessor.HttpContext.Request.Cookies[MtSiteConsts.CookieNames.SessionCookieName];
            //if (sessionId == null)
            //{
            //    throw new UserFriendlyException("用户浏览器不支持Cookie");//本地化
            //}
            //string code = _cacheManager.GetVerificationCodeCache().Get(sessionId, () => null);
            //_cacheManager.GetVerificationCodeCache().Remove(sessionId);
            //if (code == null)
            //    return ResultType.WrongVerifyCode;
            //if (code.ToLower() != input.valiCode.ToLower())
            //{
            //    return ResultType.WrongVerifyCode;
            //}
            var Messages = new Messages
            {
                Title = input.Title,
                Name = input.Name,//账号
                MenuId = input.MenuId,
                //LoginName = input.LoginName,  //登录名
                //IndustryRole = input.IndustryRole,//行业角色
                //CompanyName = input.CompanyName, // 公司名
                //CompanyURL = input.CompanyURL, // 公司网址
                //Contact = input.Contact,
                //Industry = input.Industry, //行业
                Intro = input.Intro, //咨询内容
                EMail = input.EMail,
                IsActive = false,
                //SourceMenuId = input.SourceMenuId,
                //SourceUrl = input.SourceUrl.Replace("cn/zh-cn/", ""),
                //RefererUrl = input.RefererUrl.Replace("cn/zh-cn/",""),
                Subhead=input.Subhead,
                //Ip = _httpContextAccessor.HttpContext?.Connection.RemoteIpAddress.ToString()
            };
            await _messagesManager.CreateAndGetIdAsync(Messages);

            return ResultType.Success;
        }
    }
}
